package com.example.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.example.demo.mybatis.domain.TCust;

@Api(tags = "6 XML格式的请求和响应")
@RestController
@RequestMapping("/xml")
public class XmlBodyController {

    /**
     *  引入依赖：
     *  <dependency>
     *      <groupId>com.fasterxml.jackson.dataformat</groupId>
     *      <artifactId>jackson-dataformat-xml</artifactId>
     *  </dependency>
     *  实体添加注释：
     *  @JacksonXmlRootElement、@JacksonXmlProperty注解是用来维护对象属性在xml中的对应关系。
     *  接口Mapping添加：
     *  接口Mapping添加：
     *  consumes = MediaType.APPLICATION_XML_VALUE,
     *  produces = MediaType.APPLICATION_XML_VALUE
     *
     *
     * xml并返回xml
     * <cust>
     *     <name>aaa</name>
     *     <age>10</age>
     * </cust>
     *
     * <cust>
     *     <deleted/>
     *     <id/>
     *     <name>didispace.com : aaa</name>
     *     <age>110</age>
     *     <email/>
     * </cust>
     */
    @ApiOperation(value = "xml交互接口")
    @PostMapping(value = "/cust",
            consumes = MediaType.APPLICATION_XML_VALUE,
            produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public TCust create(@RequestBody TCust cust) {
        cust.setName("xml: " + cust.getName());
        cust.setAge(cust.getAge() + 100);
        return cust;
    }

}
